/**
 * 
 */
package com.tbdgroup.babycare.util;

import android.annotation.SuppressLint;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author wsf
 *
 */
public class Utils {

	/**
	 * get the date string of the @param before day before @param baseDateString
	 * for example, input is the string of 20130820 11:03:50, 7
	 * the result is the string of 20130813 00:00:00
	 * @param baseDateString
	 * @param before
	 * @return
	 */
	public static String dateStringNDaysBefore(String baseDateString, int before) {
		try {
			long base = Long.parseLong(baseDateString);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date(base));
			calendar.add(Calendar.DAY_OF_MONTH, -before);
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			return calendar.getTime().getTime() + "";
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	@SuppressLint("SimpleDateFormat")
	public static String dateStringByFormat(Date date, String format) {
		if(date == null) {
			return "";
		}
		format = format == null ? "yyyyMMdd" : format;
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(date);
	}
}
